{include file="base/header"}
    
        <div class="layui-card">
            <div class="layui-card-body">
                <table id="tableId" lay-filter="user-table"></table>
            </div>
        </div>
            <script type="text/html" id="user-toolbar">
                <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
                    <i class="layui-icon layui-icon-add-1"></i>
                    新增角色
                </button>
            </script>

        <script type="text/html" id="user-set">
        			<button class="pear-btn pear-btn-primary pear-btn-xs" lay-event="edit">编辑</button>
                    {{#if (d.id >2) { }}
        		    <button class="pear-btn pear-btn-danger pear-btn-xs" lay-event="delete">删除</button>
                    {{#} }}
        </script>
        <script>
			layui.use(['table', 'form', 'jquery','common'], function() {
				let table = layui.table;
				let form = layui.form;
				let $ = layui.jquery;
				let common = layui.common;
                let cols = [
                    [{
                        title: 'ID',
                        field: 'id',
                        align: 'center',
                        width: 50
                    },
                    {
                            title: '角色名称',
                            field: 'name',
                            align: 'center',
                           width: 200
                          
                        },
                    {
                        title: '权限人数',
                        field: 'count',
                        align: 'center',
                        width: 200
                    },
       
                    {
                        title: '添加时间',
                        field: 'addtime',
                        align: 'center'
                    },
                    {
                        title: '操作',
                        toolbar: '#user-set',
                        align: 'left',
                        width: 150
                    }
                    ]
                ];
                table.render({
                    elem: '#tableId',
                    url: "{:url('admin/rolelst')}",
                    page: true,
                    cols: cols,
                    skin: 'line',
                    toolbar: '#user-toolbar',
                    defaultToolbar: [{
                        title: '刷新',
                        layEvent: 'refresh',
                        icon: 'layui-icon-refresh',
                    }, 'filter',  'exports']
                });
                
                table.on('tool(user-table)', function (obj) {
                    if (obj.event === 'edit') {
                 
                        window.edit(obj.data);
                    }
                    if (obj.event === 'delete') {

                        window.delete(obj);
                    }
                });
                table.on('toolbar(user-table)', function (obj) {
                    if (obj.event === 'add') {

                        window.add();
                    }else if (obj.event === 'refresh') {
						window.refresh();
					}
                });
                window.refresh = function(param) {
					table.reload('tableId');
				}

                window.add = function () {

                    layer.open({
                        type: 2,
                        title: '新增角色',
                        shade: 0.1,
                        area: ['800px', '600px'],
                        content: "{:url('admin/roleedit')}?id=0"
                    });
                }
                window.edit = function (data) {
                         
                    layer.open({
                        type: 2,
                        title: '修改角色->'+ data.name,
                        shade: 0.1,
                        area: ['800px', '600px'],
                        content: "{:url('admin/roleedit')}?id="+data.id
                    });
                }
                window.delete = function (obj) {
                    var id=obj.data.id;
                    layer.confirm('确定要删除该角色->'+obj.data.name, {
                        icon: 3,
                        title: '提示'
                    }, function (index) {
                        layer.close(index);
                        loading=layer.load();
                        postData("{:url('admin/roleDelete')}",{id:obj.data.id}).then(function(res){
                           layer.close(loading);
                     
                            if(res.code==200){
                                layer.msg(res.message, {
                                    icon: 1,
                                    time: 1000
                                }, function () {
                                    obj.del();
                                });
                                return;
                            }
                            layer.msg(res.message, {
                                icon: 2,
                                time: 1000
                            });
                        });
                    });
                    
                }

                form.on('submit(user-query)', function (data) {
                    table.reload('tableId', {
                        page: {curr: 1 },
                        where: data.field
                    })
                    return false;
                });

			})
		</script>
{include file="base/footer"}